package com.imy.accessibility.node;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.imy.accessibility.R;
import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.util.UiUtil;

/**
 * create by Imy
 */
public class FloatingView extends BaseCustomViewGroup {
  public FloatingView(Context context) {
    super(context);
  }

  public FloatingView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public FloatingView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }
  public void setOrientation(int orientation) {
    this.ori = orientation;
  }
  private int ori = Configuration.ORIENTATION_PORTRAIT;
  private float downX;
  private float downY;
  @Override
  protected void init(View inflate) {
    setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            downX = event.getRawX();
            downY = event.getRawY();
            break;
          case MotionEvent.ACTION_MOVE:
            float rawX = event.getRawX();
            float rawY = event.getRawY();
            if (Math.abs(downX - rawX) < 10 && Math.abs(downY - rawY) < 10) {
              //过滤掉抖动
              break;
            }
            //直接更新xy
            int x;
            int width = getMeasuredWidth() / 2;
            if (ori == Configuration.ORIENTATION_LANDSCAPE) {
              x = (int) event.getRawX() - getMeasuredWidth();
            } else {
              x = (int) event.getRawX() - width;
            }
            if (x < 0) {
              x = 0;
            }
            int y;
            if (ori == Configuration.ORIENTATION_LANDSCAPE) {
              int i = getMeasuredHeight() / 2;
              y = (int) event.getRawY() - i;
            } else {
              y = (int) event.getRawY() - getMeasuredHeight();
            }
            if (y < 0) {
              y = 0;
            }
            setX(x);
            setY(y);
            break;
          case MotionEvent.ACTION_UP:
            rawX = event.getRawX();
            rawY = event.getRawY();
            if (Math.abs(downX - rawX) < 10 && Math.abs(downY - rawY) < 10) {
              onClicked();
            } else {
              //判断是否需要依附到边上
              checkMove(rawX, rawY);
            }
            break;
        }
        return true;
      }
    });
    setY(UiUtil.getDeviceHeight(mContext)/3);
    measure(0,0);
    setX(UiUtil.getDeviceWidth(mContext)-getMeasuredWidth());
  }
  public static final int LEFT = 0;
  public static final int RIGHT = 1;
  private int mCurrentStatus;
  private void checkMove(float rawX, float rawY) {
    //判断是否需要依附到边上
    int deviceWidth = UiUtil.getDeviceWidth(getContext());
    int width_half = deviceWidth / 2;

    if (rawX < width_half) {//靠左边
      mCurrentStatus = LEFT;
      startXAnim((int) rawX, 0);
    } else {
      mCurrentStatus = RIGHT;
      startXAnim((int) rawX, deviceWidth - getMeasuredWidth());
    }
  }
  private boolean inAnim = false;
  private void startXAnim(int start, int end) {
    inAnim = true;
    ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        Context context = getContext();
        if (context instanceof Activity) {
          Activity activity = (Activity) context;
          if (activity.isFinishing()) {
            return;
          }
        }
        int animatedValue = (int) animation.getAnimatedValue();
        setX(animatedValue);
        if (animation.getAnimatedFraction() == 1) {
          inAnim = false;

        }
      }
    });
    valueAnimator.start();
  }

  private void onClicked() {
    if (listener!=null){
      listener.onClicked();
    }
  }

  @Override
  protected int getLayoutID() {
    return R.layout.custom_view_floating;
  }
  public interface FloatingListener {
    void onClicked();
  }

  public void setFloatingListener(FloatingListener listener) {
    this.listener = listener;
  }

  private FloatingListener listener;
}
